package com.latsen.pawfit.ext

import android.app.Activity
import android.content.Context
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.support.v7.app.AppCompatActivity
import android.view.inputmethod.InputMethodManager
import com.hjsmallfly.syllabus.App
import com.tbruyelle.rxpermissions.RxPermissions


@Suppress("DEPRECATION")
val Activity.deviceWidth
    get() = windowManager.defaultDisplay.width

@Suppress("DEPRECATION")
val Activity.deviceHeight
    get() = windowManager.defaultDisplay.height

fun FragmentActivity.allowPermissions(vararg permissions: String) = RxPermissions.getInstance(this)
    .request(*permissions)

fun AppCompatActivity.addFragment(frameId: Int, fragment: Fragment) {
    supportFragmentManager.beginTransaction().run {
        add(frameId, fragment)
        commitAllowingStateLoss()
    }
}

fun AppCompatActivity.addFragmentToStack(frameId: Int, fragment: Fragment, tag: String? = null) {
    supportFragmentManager.beginTransaction().run {
        add(frameId, fragment, tag)
        addToBackStack(tag)
        commitAllowingStateLoss()
    }
}

fun Activity.hideInput() {
    val imm = App.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
    imm?.hideSoftInputFromWindow(window.decorView.windowToken, 0)
}
